package com.aaa.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.entity.Result;
import com.aaa.service.UserService;
import com.aaa.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/9 9:20
 * @description：用户业务控制器
 * @modified By：
 * @version: 1.0
 */
@WebServlet("/user")
public class UserController extends BaseController {
    UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从基类中调用通用的获取参数的方法
        Map map = super.getParams(req);
        System.out.println(map);
        //获取method的参数值
        String method = map.get("method") + "";
        switch (method) {
            case "add":
                boolean saveUser = userService.saveUser(map);
                if (saveUser) {
                    super.success(resp);

                } else {
                    fail(resp);
                }
                break;
            case "delete":
                boolean delete = userService.deleteUser(map);
                if (delete) {
                    super.success(resp);
                } else {
                    fail(resp);
                }
                break;
            case "update":
              /*  boolean updateUser = userService.updateUser(map);
                if (updateUser) {
                    this.findAll(map, req, resp);
                } else {
                    //去错误页面
                    req.getRequestDispatcher("error.jsp").forward(req, resp);
                }*/
                break;
            case "toUpdate":
                break;
            case "queryUserByName":
                //根据用户名查询单个用户
                List<Map<String, Object>> userList = userService.queryUserByName(map);
                if(ObjectUtil.isNotEmpty(userList)){
                    //用户名重复
                    fail(resp,"用户名"+map.get("username")+"重复，请确认！");
                }else{
                    //用户名可用
                    success(resp);
                }
                break;
            default:
                List<Map<String, Object>> mapList = userService.queryAll(map);
                Long count = userService.queryCount(map);
                super.success(resp, mapList, count);
        }
    }


}
